/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.User;
import persistence.PersistenceInterface;
import persistence.PersistenceFactory;
/**
 *
 * @author isen
 */
public class LoginServlet extends CoolServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("pass");
        PersistenceInterface persistenceManager = PersistenceFactory.getInstance(persistenceMechanism);
        
        if (persistenceManager.checkCombination(username, password)&&persistenceManager.isClient(username)) {
            gotoURL(clientview, request, response);
            request.getSession().setAttribute ("user", username);
            request.getSession().setAttribute ("status", "client");
            
        } else if(persistenceManager.checkCombination(username, password)&& !persistenceManager.isClient(username)){
            gotoURL(administration, request, response);
            request.getSession().setAttribute ("status", "admin");
            
        }else{    
            request.setAttribute("resultado","wrong combination username/password");
            request.getRequestDispatcher("forms/LoginForm.jsp").forward(request, response);
        }
    
    }

}
